/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sender.ui;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFRenderer;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

/**
 *
 * @author parent
 */
public class PDFViewFrame extends javax.swing.JFrame {

    /**
     * Creates new form PDFViewFrame
     */
    private RandomAccessFile raf;
    private PDFFile pdfFile;
    private int pagenum=1;
    static Image image=null;
    private String NamaFile;
    public PDFViewFrame() {
        //initComponents();
        JLabel label = new JLabel (new ImageIcon (image));
        label.setVerticalAlignment (JLabel.TOP);

      setContentPane (new JScrollPane (label));

      pack ();
        
        scrollMain.add(label);
    }
    public void setPDFName(String pdf_file){
        NamaFile=pdf_file;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TopToolbar = new javax.swing.JToolBar();
        btnSend = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnFirst = new javax.swing.JButton();
        btnPrev = new javax.swing.JButton();
        txtPageNo = new javax.swing.JTextField();
        btnNext = new javax.swing.JButton();
        btnLast = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnClose = new javax.swing.JButton();
        scrollMain = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        TopToolbar.setRollover(true);

        btnSend.setText("Kirim");
        btnSend.setFocusable(false);
        btnSend.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSend.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TopToolbar.add(btnSend);

        btnClear.setText("Hapus");
        btnClear.setFocusable(false);
        btnClear.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnClear.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TopToolbar.add(btnClear);
        TopToolbar.add(jSeparator1);

        btnFirst.setText("Pertama");
        btnFirst.setFocusable(false);
        btnFirst.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnFirst.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TopToolbar.add(btnFirst);

        btnPrev.setText("Sebelum");
        btnPrev.setFocusable(false);
        btnPrev.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnPrev.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TopToolbar.add(btnPrev);

        txtPageNo.setText("0");
        txtPageNo.setPreferredSize(new java.awt.Dimension(4, 20));
        TopToolbar.add(txtPageNo);

        btnNext.setText("Sesudah");
        btnNext.setFocusable(false);
        btnNext.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNext.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TopToolbar.add(btnNext);

        btnLast.setText("Akhir");
        btnLast.setFocusable(false);
        btnLast.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnLast.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TopToolbar.add(btnLast);
        TopToolbar.add(jSeparator2);

        btnClose.setText("Tutup");
        btnClose.setFocusable(false);
        btnClose.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnClose.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });
        TopToolbar.add(btnClose);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TopToolbar, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
            .addComponent(scrollMain)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(TopToolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollMain, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
            // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    /**
     * @param args the command line arguments
     */
    public void DrawPDF(){
      try {
            raf = new RandomAccessFile (new File (NamaFile), "r");
            FileChannel fc = raf.getChannel ();
            ByteBuffer buf = fc.map (FileChannel.MapMode.READ_ONLY, 0, fc.size ());
            pdfFile = new PDFFile (buf);
            int numpages = pdfFile.getNumPages ();
            txtPageNo.setText("1/"+Integer.toString(numpages));
            PDFPage page = pdfFile.getPage (pagenum);
              
            Rectangle2D r2d = page.getBBox ();
            Graphics2D g2=(Graphics2D)scrollMain.getGraphics();
            double width = r2d.getWidth ();
            double height = r2d.getHeight ();
            width /= 72.0;
            height /= 72.0;
            int res = Toolkit.getDefaultToolkit ().getScreenResolution ();
            width *= res;
            height *= res;
            image = page.getImage ((int) width, (int) height, r2d, null, true, true);
            PDFRenderer pdfr=new PDFRenderer(page, g2, null, r2d, Color.yellow);
            page.waitForFinish();
            pdfr.run();
        } catch (Exception ex){
            ex.printStackTrace();
        }  
      
    }
    public static void main(String args[]) throws Exception {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PDFViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PDFViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PDFViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PDFViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
         
        
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                
                new PDFViewFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar TopToolbar;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnFirst;
    private javax.swing.JButton btnLast;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrev;
    private javax.swing.JButton btnSend;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JScrollPane scrollMain;
    private javax.swing.JTextField txtPageNo;
    // End of variables declaration//GEN-END:variables
}
